#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:homework8_3_compare2.py
# author:wmf
# datetime:2021/5/8 23:19
# software: PyCharm
'''
this is function description
'''
# import module your need

import math
import time
from multiprocessing import Pool


def if_prime(n):
    if n == 1:
        return False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True


def getPrimeNum1(n):
    count = 0
    for x in range(n, n+25000):
        if if_prime(x):
            count += 1
    return count


def getPrimeNum2(n):
    count = 0
    for x in range(n, n+10000):
        if if_prime(x):
            count += 1
    return count


if __name__ == "__main__":
    p1 = Pool(4)
    start_time = time.time()
    for i in range(4):
        i = 1 + i * 25000
        p1.apply_async(getPrimeNum1, (i,))
    p1.close()
    p1.join()
    end_time = time.time()
    print("使用4个进程的运行时间为:%s" % (end_time-start_time))

    p2 = Pool(10)
    start_time = time.time()
    for i in range(10):
        i = 1 + i * 10000
        p2.apply_async(getPrimeNum2, (i,))
    p2.close()
    p2.join()
    end_time = time.time()
    print("使用10个进程的运行时间为:%s" % (end_time - start_time))
